package com.example.taskschedule.infrastructure.threadPool;

import com.example.taskschedule.domain.task.MyTask;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义拒绝策略
 * 丢弃
 */
@Slf4j
public class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if(r instanceof MyTask){
            MyTask task = (MyTask) r;
            String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            log.info("{}，任务被拒绝：{}", timestamp,task.getTaskId());
            StatisticsCollector.recordTaskRejection();
        }
    }
}
